from models.Comment import Comment
from models.user import User
from models.weibo import Weibo
from routes.session import session
from utils import redirect, template, http_response, error, log, response_with_headers


def current_user(request):
    session_id = request.cookies.get('user', '')
    # 被替换username = session.get(session_id, '【游客】')
    user_id = session.get(session_id, -1)
    return user_id


def index(request):
    # 主页处理函数,返回主页的响应
    user_id = request.query.get('user_id', -1)
    user_id = int(user_id)
    user = User.find_by(id=user_id)
    if user is None:
        return redirect('/login')
    weibo_list = Weibo.find_all(user_id=user_id)
    body = template('weibo_index.html', weibos=weibo_list, user=user)
    return http_response(body)


def new(request):
    uid = current_user(request)
    user = User.find_by(id=uid)
    body = template('weibo_new.html')
    return http_response(body)


def add(request):
    uid = current_user(request)
    # user = User.find_by(id=uid)
    # if request.method == 'POST':
    user = User.find_by(id=uid)
    form = request.form()
    # t = weibo.new(form)
    w = Weibo(form)
    w.user_id = user.id
    w.save()
    # return redirect('/weibo')
    return redirect('/weibo/index?user_id={}'.format(user.id))


def delete(request):
    # uname = current_user(request)
    uid = current_user(request)
    # u = User.find_by(username=uname)
    user = User.find_by(id=uid)
    # if u is None:
    #     return redirect('/login')
    # weibo_id = int(request.query.get('id', -1))
    weibo_id = int(request.query.get('id', None))
    w = Weibo.find_by(id=weibo_id)
    # if t.user_id != u.id:
    #     return redirect('/login')
    # if t is not None:
    #     t.remove()
    # 错误的w.delete(id=weibo_id)
    w.delete()
    return redirect('/weibo/index?user_id={}'.format(user.id))


def edit(request):
    # headers = {
    #     'Content-Type': 'text/html',
    # }
    # uname = current_user(request)
    # u = User.find_by(username=uname)
    # if u is None:
    #     return redirect('/login')
    weibo_id = int(request.query.get('id', -1))
    w = Weibo.find_by(id=weibo_id)
    # if t.user_id != u.id:
    #     return redirect('/login')
    # body = template('weibo_edit.html')
    # body = body.replace('{{weibo_id}}', str(t.id))
    # body = body.replace('{{weibo_title}}', str(t.title))
    if w is None:
        return error(request)
    body = template('weibo_edit.html', weibo_id=w.id, weibo_content=w.content)
    # header = response_with_headers(headers)
    # r = header + '\r\n' + body
    # return r.encode(encoding='utf-8')
    return http_response(body)


def update(request):
    # weibo_id = int(request.query.get('id'))
    # # uname = current_user(request)
    # # u = User.find_by(username=uname)
    # # if u is None:
    # #     return redirect('/login')
    # # if request.method == 'POST':
    # form = request.form()
    # #     weibo_id = int(form.get('id', -1))
    # t = weibo.find_by(id=weibo_id)
    # #     t.title = form.get('title', t.title)
    # t.task = form.get('task')
    # t.save()
    # return redirect('/weibo/index')

    username = current_user(request)
    user = User.find_by(username=username)
    form = request.form()
    content = form.get('content', '')
    weibo_id = int(form.get('id', -1))
    w = Weibo.find(weibo_id)
    if user.id != w.user_id:
        return error(request)
    w.content = content
    w.save()
    # 重定向到用户的主页
    return redirect('/weibo/index?user_id={}'.format(user.id))


def comment_add(request):
    headers = {
        'Content-Type': 'text/html',
    }
    uid = current_user(request)
    header = response_with_headers(headers)
    user = User.find(uid)
    # 创建微博
    form = request.form()
    w = Comment(form)
    w.user_id = user.id
    w.save()
    return redirect('/weibo/index?user_id={}'.format(user.id))


# 定义一个函数统一检测是否登录
def login_required(route_function):
    def func(request):
        uid = current_user(request)
        log('登录鉴定, user_id ', uid)
        if uid == -1:
            # 没登录 不让看 重定向到 /login
            return redirect('/login')
        else:
            # 登录了, 正常返回路由函数响应
            return route_function(request)

    return func


route_dict = {
    '/weibo/index': index,
    '/weibo/new': login_required(new),
    '/weibo/add': login_required(add),
    '/weibo/edit': login_required(edit),
    '/weibo/update': login_required(update),
    '/weibo/delete': login_required(delete),
    # 评论功能
    '/comment/add': login_required(comment_add),
}
